<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/9/11
 * Time: 15:52
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Publicclass extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if(request()->isAjax()) {
            $params = input('param.');
            $where = [];
            // 公开课名称
            if(isset($params['public_name']) && $params['public_name'] != '') {
                $where['public_name'] = ['like', '%' . $params['public_name'] . '%'];
            }
            // 老师昵称
            if(isset($params['user_username']) && $params['user_username'] != ''){
                $where['user_username'] = ['like', '%' . $params['user_username'] . '%'];
            }
            // 费用类型
            if(isset($params['public_type']) && $params['public_type'] != ''){
                $where['public_type'] = $params['public_type'];
            }
            // 课程分类
            if(isset($params['public_subject']) && $params['public_subject'] != ''){
                $where['public_subject'] = $params['public_subject'];
            }
            // 是否精品
            if(isset($params['is_hot']) && $params['is_hot'] != ''){
                $where['is_hot'] = $params['is_hot'];
            }
            // 年级
            if(isset($params['public_grade']) && $params['public_grade'] != ''){
                $where['public_grade'] = $params['public_grade'];
            }
            // 发布端
            if(isset($params['public_from']) && $params['public_from'] != ''){
                $where['public_from'] = $params['public_from'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_public_class')
                ->join('up_u_user', 'user_id = public_teacher_id', 'left')
                ->join('up_subject', 'subject_id = public_subject', 'left')
                ->join('up_grade', 'grade_id = public_grade', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_public_class')
                ->join('up_u_user', 'user_id = public_teacher_id', 'left')
                ->join('up_subject', 'subject_id = public_subject', 'left')
                ->join('up_grade', 'grade_id = public_grade', 'left')
                ->field('public_id, public_name, public_img, subject_name, grade_name, 
                public_type, is_hot, public_is_on, user_username, public_price, public_teacher_id, 
                public_from, public_is_top')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('public_is_top asc, public_time desc')
                ->select();
            foreach($lists as $k => $v){
                $num = Db::table('up_public_apply')->where('apply_public_id', $v['public_id'])->count();
                $lists[$k]['num'] = $num;
                if($v['public_teacher_id'] == -1){
                    $lists[$k]['user_username'] = 'admin';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 发布公开课
     * @return mixed|\think\response\Json
     */
    public function addPublicClass(){
        if(Request::instance()->isPost()){
            $params = input('');
            $insert = [
                'public_from' => 2,
                'public_teacher_id' => -1,
                'public_name' => $params['public_name'],
                'public_subject' => $params['public_subject'],
                'public_grade' => $params['public_grade'],
                'public_type' => $params['public_type'],
                'public_intro' => $params['public_intro'],
                'public_img' => $params['public_img'],
                'public_time' => date('Y-m-d H:i:s'),
                'is_hot' => $params['is_hot'],
                'public_is_on' => $params['public_is_on']
            ];
            if($params['public_type'] == 1){
                $insert['public_price'] = 0;
            }else{
                $insert['public_price'] = $params['public_price'];
            }
            $res = Db::table('up_public_class')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '发布成功']);
            }else{
                return json(['code' => 100, 'msg' => '发布失败']);
            }
        }
        // 学科
        $subject = Db::table('up_subject')->field('subject_id, subject_name')->order('subject_sort asc')->select();
        // 年级
        $grade = Db::table('up_grade')->field('grade_id, grade_name')->select();
        $this->assign([
            'subject' => $subject,
            'grade' => $grade
        ]);
        return $this->fetch('publicclass/addpublicclass');
    }
    
    /**
     * 公开课详情
     * @return mixed
     */
    public function classDetail(){
        $public_id = input('public_id');
        $info = Db::table('up_public_class')
            ->join('up_u_user', 'user_id = public_teacher_id', 'left')
            ->join('up_subject', 'subject_id = public_subject', 'left')
            ->join('up_grade', 'grade_id = public_grade', 'left')
            ->field('public_id, public_name, public_img, subject_name, grade_name, 
                public_type, is_hot, public_is_on, user_username, public_price, public_intro, 
                user_intro, user_id, user_grade, public_from')
            ->where('public_id', $public_id)
            ->find();
        // 星级
        $star = $this->teacherStar($info['user_id']);
        // 报名人数
        $num = Db::table('up_public_apply')->where('apply_public_id', $info['public_id'])->count();
        // 教学年级
        $grade = Db::table('up_grade')->where('grade_id', $info['user_grade'])->value('grade_name');
        $this->assign([
            'star' => $star,
            'num' => $num,
            'info' => $info,
            'grade' => $grade
        ]);
        return $this->fetch('publicclass/classdetail');
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function classDown(){
        $public_id = input('id');
        $res = Db::table('up_public_class')->where('public_id', $public_id)->setField('public_is_on', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function classOn(){
        $public_id = input('id');
        $res = Db::table('up_public_class')->where('public_id', $public_id)->setField('public_is_on', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 取消精品公开课
     * @return \think\response\Json
     */
    public function classUnhot(){
        $public_id = input('id');
        $res = Db::table('up_public_class')->where('public_id', $public_id)->setField('is_hot', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置精品公开课
     * @return \think\response\Json
     */
    public function classHot(){
        $public_id = input('id');
        $res = Db::table('up_public_class')->where('public_id', $public_id)->setField('is_hot', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除公开课
     * @return \think\response\Json
     */
    public function classDel(){
        $public_id = input('id');
        $res = Db::table('up_public_class')->where('public_id', $public_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 节
     * @return mixed|\think\response\Json
     */
    public function sectionIndex()
    {
        $public_id = input('public_id');
        if($public_id != ''){
            session('public_id', $public_id);
        }
        if(request()->isAjax()) {
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_public_section')
                ->join('up_public_class', 'public_id = section_public_id', 'left')
                ->where('section_public_id', session('public_id'))
                ->count();// 获取总条数
            $lists = Db::table('up_public_section')
                ->join('up_public_class', 'public_id = section_public_id', 'left')
                ->where('section_public_id', session('public_id'))
                ->field('section_id, section_name, section_type, section_video, public_from')
                ->page($Nowpage, $limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $class = Db::table('up_public_class')
            ->field('public_from')
            ->where('public_id', session('public_id'))
            ->find();
        $this->assign([
           'class' => $class
        ]);
        return $this->fetch('publicclass/sectionindex');
    }
    
    /**
     * 添加节
     * @return mixed|\think\response\Json
     */
    public function addSection(){
        set_time_limit (0);
        if(Request::instance()->isPost()){
            $params = input('');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传视频']);
            }
            //转码的文件url
            $url = config('qiniu.domain') . $params['video'];//未转码之前的视频url，用于获取视频信息
            $video_info = file_get_contents($url . '?avinfo');
            $video_info = json_decode($video_info,true);
            $video_long_array = explode('.', $video_info['format']['duration']); // 时长
            $video_size = $video_info['format']['size']; // 大小
            // 转码
            $res = $this->zhuanma($params['video']);
            $insert = [
                'section_public_id' => session('public_id'),
                'section_name' => $params['section_name'],
                'section_type' => $params['public_type'],
                'section_video' => config('qiniu.domain') . 'video_' . $params['video'],
                'section_size' => $video_size,
                'section_long_time' => $video_long_array[0],
                'section_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_public_section')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('publicclass/addsection');
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function sectionEdit(){
        $section_id = input('section_id');
        if(Request::instance()->isPost()){
            $params = input('');
            $update = [
                'section_name' => $params['section_name'],
                'section_type' => $params['public_type']
            ];
            if(!empty($params['video'])){
                //转码的文件url
                $url = config('qiniu.domain') . $params['video'];//未转码之前的视频url，用于获取视频信息
                $video_info = file_get_contents($url . '?avinfo');
                $video_info = json_decode($video_info,true);
                $video_long_array = explode('.', $video_info['format']['duration']); // 时长
                $video_size = $video_info['format']['size']; // 大小
                // 转码
                $this->zhuanma($params['video']);
                $update['section_video'] = config('qiniu.domain') . 'video_' . $params['video'];
                $update['section_size'] = $video_size;
                $update['section_long_time'] = $video_long_array[0];
            }
            $res = Db::table('up_public_section')->where('section_id', $params['section_id'])->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $section = Db::table('up_public_section')
            ->field('section_name, section_type, section_video, section_size')
            ->where('section_id', $section_id)
            ->find();
        $section['section_size'] = $this->trans_byte($section['section_size']);
        $this->assign([
            'section' => $section,
            'section_id' => $section_id
        ]);
        return $this->fetch('publicclass/sectionedit');
    }
    
    /**
     * 删除节
     * @return \think\response\Json
     */
    public function sectionDel(){
        $section_id = input('id');
        $res = Db::table('up_public_section')->delete($section_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 置顶
     * @return \think\response\Json
     */
    public function setClassTop(){
        $public_id = input('id');
        $is_top = Db::table('up_public_class')->where('public_id', $public_id)->value('public_is_top');
        if($is_top == 1){
            $public_is_top = 2;
        }else{
            $public_is_top = 1;
        }
        $res = Db::table('up_public_class')->where('public_id', $public_id)->setField('public_is_top', $public_is_top);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}